package com.googlecode.stemwijzer.client.view;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.stemwijzer.client.presenter.ResultsPresenter;
import com.googlecode.stemwijzer.client.widget.PoliticalPartyInfoDialog;
import com.googlecode.stemwijzer.shared.data.PoliticalPartyData;
import com.googlecode.stemwijzer.shared.model.IsQuestionnaireResult;
import com.googlecode.stemwijzer.shared.model.impl.PoliticalParty;

public class ResultsView extends Composite implements ResultsPresenter.Display {

	private static ResultsViewUiBinder uiBinder = GWT.create(ResultsViewUiBinder.class);

	interface ResultsViewUiBinder extends UiBinder<HTMLPanel, ResultsView> {
	}

	private static final int MAX_DIFF = 20;

	@UiField
	VerticalPanel resultsPanel;

	@UiField
	Button resetSession;

	public ResultsView() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void startProcessing() {
		resetSession.setEnabled(false);
	}

	@Override
	public void stopProcessing() {
		resetSession.setEnabled(true);
	}

	@Override
	public void showResults(final IsQuestionnaireResult result) {
		resultsPanel.clear();

		if (result != null) {
			resultsPanel.add(new HTML("<h1>Resultaten voor: " + result.getName() + "</h1>"));

			for (PoliticalParty party : PoliticalPartyData.getAll()) {
				Integer diff = result.getDifference(party.getResult());
				Log.debug("diff: " + diff);

				HorizontalPanel hp = new HorizontalPanel();
				hp.add(new Label(getPercentage(diff) + "% "));
				hp.add(createInfoLink(party));

				resultsPanel.add(hp);
			}
		} else {
			resultsPanel.add(new HTML("Resultaten niet beschikbaar"));
		}
	}

	private static final Anchor createInfoLink(final PoliticalParty politicalParty) {
		Anchor link = new Anchor(politicalParty.getAbbreviation() + " (" + politicalParty.getName() + ")");
		link.setTitle("Klik voor meer info");
		link.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				// Example of developer guided code splitting
				GWT.runAsync(new RunAsyncCallback() {

					@Override
					public void onSuccess() {
						final PoliticalPartyInfoDialog dialog = new PoliticalPartyInfoDialog(politicalParty);
						dialog.center();
						dialog.show();
					}

					@Override
					public void onFailure(Throwable t) {
						Window.alert("Info dialog could not be loaded: " + t.getMessage());
					}
				});
			}
		});
		return link;
	}

	private static final int getPercentage(final Integer value) {
		return 100 - (100 * value / MAX_DIFF);
	}

	@Override
	public HasClickHandlers getResetSession() {
		return this.resetSession;
	}
}
